﻿using System.Windows;
using System.Linq;
using PSE.Deployment.ConfigurationManager.Controls;
using PSE.Deployment.Util;
using System;

namespace PSE.Deployment.SetupBase.UI
{
    public partial class InstallationMode : BaseWindow
    {
        #region Fields
        public PSE.Deployment.SetupBase.Parameter.Enums.SetupMode SelectedSetupMode { get; set; }
        #endregion

        #region Construtor
        public InstallationMode()
        {
            this.InitializeComponent();
            this.ConfirmOnClosing = false;

            if (SetupMainFactory.GetInstance().OriginalConfiguration.SystemInfo.Version > SetupMainFactory.GetInstance().Parameter.SystemVersion)
            {
                BtnRepair.Visibility = Visibility.Visible;
                BtnRemove.Visibility = Visibility.Collapsed;
                BtnChange.Visibility = Visibility.Collapsed;
            }
            else
            {
                BtnRepair.Visibility = Visibility.Collapsed;
                BtnRemove.Visibility = Visibility.Visible;
                BtnChange.Visibility = Visibility.Visible;
            }

            if (SetupMainFactory.GetInstance().Configuration.SystemInfo.Groups.SelectDeep(g => g.SubGroups).SelectDeep(g => g.Dependencies).Count() <= 1)
            {
                BtnChange.Visibility = Visibility.Collapsed;
            }
        }
        #endregion

        #region Evento do Botão Add / Remove Programs
        private void BtnChange_Click(object sender, RoutedEventArgs e)
        {
            SelectedSetupMode = PSE.Deployment.SetupBase.Parameter.Enums.SetupMode.Change;
            this.Close(false);
        }
        #endregion

        #region Evento do Botão Reparar
        private void BtnRepair_Click(object sender, RoutedEventArgs e)
        {
            CheckPreviouslyVersion _checkPreviouslyVersion = new CheckPreviouslyVersion();

            _checkPreviouslyVersion.CheckInstalledPrograms(true);

            if (_checkPreviouslyVersion.InvalidPrograms.Count > 0)
            {
                if (ConfigurationMessageBox.Show("Existem programas abertos que precisam ser finalizados antes do processo de atualização do sistema. Deseja encerra-los e prosseguir a atualização? ", "Atenção", ConfigurationMessageBoxButton.YesNo, ConfigurationMessageBoxImage.Question) == ConfigurationMessageBoxResult.No)
                {
                    return;
                }
            }
            else if (ConfigurationMessageBox.Show("Deseja realmente atualizar todos os componentes do Sistema?", "Atenção", ConfigurationMessageBoxButton.YesNo, ConfigurationMessageBoxImage.Question) == ConfigurationMessageBoxResult.No)
            {
                return;
            }

            SelectedSetupMode = PSE.Deployment.SetupBase.Parameter.Enums.SetupMode.Repair;

            SetupMainFactory.GetInstance().Parameter.InstalationSource = System.Windows.Forms.Application.StartupPath;

            this.Close(false);
        }
        #endregion

        #region Evento do Botão Desinstalar
        private void BtnRemove_Click(object sender, RoutedEventArgs e)
        {
            CheckPreviouslyVersion _checkPreviouslyVersion = new CheckPreviouslyVersion();

            _checkPreviouslyVersion.CheckInstalledPrograms(true);

            if (_checkPreviouslyVersion.InvalidPrograms.Count > 0)
            {
                if (ConfigurationMessageBox.Show("Existem programas abertos que precisam ser finalizados antes do processo de desisntalação do sistema. Deseja encerra-los e prosseguir a desisntalação? ", "Atenção", ConfigurationMessageBoxButton.YesNo, ConfigurationMessageBoxImage.Question) == ConfigurationMessageBoxResult.No)
                {
                    return;
                }
            }
            else if (ConfigurationMessageBox.Show("Deseja realmente remover todos os componentes do Sistema?", "Atenção", ConfigurationMessageBoxButton.YesNo, ConfigurationMessageBoxImage.Question) == ConfigurationMessageBoxResult.No)
            {
                return;
            }

            SelectedSetupMode = PSE.Deployment.SetupBase.Parameter.Enums.SetupMode.Uninstall;

            this.Close(false);
        }
        #endregion
    }
}
